package com.chaosj.algorithm

class SwapNode {

    static void main(String[] args){
        def node1 = new ListNode(1)
        def node2 = new ListNode(2)
        def node3 = new ListNode(3)
        def node4 = new ListNode(4)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        System.err.println(solution(node1))
    }

    static ListNode solution(ListNode head) {
        if(head == null || head.next == null){
            return head
        }
        ListNode next = head.next
        head.next = solution(next.next)
        next.next = head
        return next
    }


    static class ListNode {
        int val
        ListNode next
        ListNode(int x) {
            val = x
        }


        @Override
        String toString() {
            return val+next.toString()
        }
    }
}
